package com.cyy.dyeing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.dyeing.common.DataStatusEnum;
import com.cyy.dyeing.exception.BusinessException;
import com.cyy.dyeing.mapper.InfoMapper;
import com.cyy.dyeing.model.Info;
import com.cyy.dyeing.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: cyy
 * @Date: 2024/7/25
 */
@Service
@Transactional
public class InfoServiceImpl extends ServiceImpl<InfoMapper, Info> implements InfoService {

    @Autowired
    private InfoMapper infoMapper;
    @Override
    public void deleteInfo(int infoType, String eid) {
        LambdaQueryWrapper<Info> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Info::getInfo_type, infoType)
                .like(Info::getInfo_id, eid);
        remove(queryWrapper);
    }

    @Override
    public String infoChangeStatus(int infoType, String infoId, String infoStatus) {
        String targetStatus = null;
        String msg = getInfoTypeName(infoType);
        if(DataStatusEnum.UNDETERMINED.getCode().equals(infoStatus)){
            targetStatus = DataStatusEnum.DETERMINED.getCode();
            msg += "确定成功";
        } else if (DataStatusEnum.DETERMINED.getCode().equals(infoStatus)) {
            targetStatus = DataStatusEnum.ACTIVE.getCode();
            msg += "启用成功";
        } else if (DataStatusEnum.ACTIVE.getCode().equals(infoStatus)) {
            targetStatus = DataStatusEnum.DISABLED.getCode();
            msg += "禁用成功";
        }else if(DataStatusEnum.DISABLED.getCode().equals(infoStatus)){
            targetStatus = DataStatusEnum.ACTIVE.getCode();
            msg += "启用成功";
        }
        if(targetStatus == null){
            throw new BusinessException("状态不正确");
        }
        int r = infoMapper.updateStatus(infoId, infoStatus, targetStatus);
        if(r > 0){
            return msg;
        }
        throw new BusinessException("更新" + infoId + "状态失败, 请联系管理员");
    }
    private String getInfoTypeName(int infoType){
        switch (infoType){
            case 1:
                return "客户信息";
            case 2:
                return "规格信息";
            case 3:
                return "花色信息";
            default:
                return null;
        }
    }
}
